1701C - Schedule Management - CodeForces Solution


binary search greedy implementation two pointers *1400

Please click on ads to support us..

Python Code:

t=int(input())

for _ in range(t):
    n,m =[int(i) for i in input().split()]
    a=[int(i) for i in input().split()]
    worker=[0]*n
    for x in a:
        worker[x-1]+=1
    
    high=max(worker)
    low=0
    
    while low<=high:
        mid=low+(high-low)//2
        tasks=0
        for x in worker:
            if x>=mid:
                tasks+=mid
            else:
                tasks+=x+(mid-x)//2
        
        if tasks>=m:
            high=mid-1
        else:
            low=mid+1
    
    print(low)
        

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define max1 300021
#define gcd __gcd
const ll mod=1e9+7;
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
const ll INF = 0x3f3f3f3f;
#define nx x+fx[i][0]
#define ny y+fx[i][1]
#define pb push_back
#define pii pair<int,int>
const ll N=3e5+10;
ll m,ans,k,d,t,c,cnt,n,l,r,i,j;	
string strr,str;
ll len=0,gs=0,min_=INF,sum=0;
int max_;
ll fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int t1[max1];
inline ll lowbit(ll x){return x&(-x);}
inline ll ls(ll x){return x<<1;}
inline ll rs(ll x){return x<<1|1;}
void update(ll x,ll d){while(x<=n){t1[x]+=d;x+=lowbit(x);}}
inline ll qsum(ll x){ll ans=0;while(x>0){ans+=t1[x];x-=lowbit(x);}return ans;}
struct edge{
       int u,v,w,nex;
}e[max1];
struct node{
	int u,val;
};
ll head[max1],a[max1],s[max1],in[max1],dp[205][205];
int st,ed;
void add(int x,int y,int z)
{
    e[++cnt].u=x;
    e[cnt].v=y;
    e[cnt].w=z;
    e[cnt].nex=head[x];
    head[x]=cnt;
}
inline void solve() {
       cin>>n>>m;ans=1;
       fill(a+1,a+n+1,0);
       for(int i=1;i<=m;i++) cin>>c,a[c]++;
//       sort(s+1,s+n+1);
       int l=1,r=m*2;
 //      cout<<r<<"\n";
  auto check = [&](int t) {
    int64_t r = m;
    for (int i = 1;i <= n;i++) {
      if (a[i] < t) r -= a[i] + (t - a[i]) / 2;
      else r -= t;
    }
    return r <= 0;
  }; 
       while(l<r){
	   	int mid=(l+r)/2;
	   	if(check(mid)) r=mid;
	   	else l=mid+1;
//	   	cout<<r<<endl;
	   }
	   cout<<l<<"\n";
}  
int main() {
     cin.tie(nullptr) -> sync_with_stdio(false); 
	 cout.tie(0);cout.setf(ios::fixed);

   	 int __=1;
    cin>>__;	  
    while(__--)
	{
	  solve();
	} 	
      return 0;
}


Comments

Submit
0 Comments
More Questions

1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set